package com.hliushi.mp;

import com.hliushi.mp.config.ThreadPoolProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author llhuang10
 * @date 2021/8/28 16:08
 */
@Slf4j
@SpringBootApplication
@EnableConfigurationProperties(value = {ThreadPoolProperties.class})
public class MpApplication {

    /**
     * 那想想你在 application.yml中配置的那些有关于classpath的配置,
     * 想一个问题: 项目的静态资源放哪? 在WebProperties.java 中有这么一段代码
     * #
     * springboot中的classpath就是在这里!, 也就是classes文件夹
     * D:\IdeaProjects\springboot-demo\mybatis-plus-learn\target\classes;
     * #
     * 对于springboot项目来说,
     * classpath指的是src/main/java和src/main/resources路径, 以及第三方jar包的根路径
     * 存放在这两个路径下的文件, 都可以通过classpath作为相对路径来引用
     * #
     * src/main/java和src/main/resources路径内容, 在执行了maven的compile后,
     * 都会被放到target/classes目录下, 里面包括.class文件, 静态资源文件 (图片, css, js文件等)
     */
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/META-INF/resources/",
            "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

    public static void main(String[] args) throws UnknownHostException {

        ConfigurableApplicationContext context = SpringApplication.run(MpApplication.class, args);

        System.out.println(System.getProperty("java.class.path"));
        // 启动完成后, 在控制台提示项目启动成功, 并且输出当前服务对应的swagger接口文档地址
        // environment 可以获取配置文件中的信息
        ConfigurableEnvironment env = context.getEnvironment();
        String appName = env.getProperty("spring.application.name");
        // 会获得局域网的ip, 会不准确
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        //
        String port = StringUtils.isEmpty(env.getProperty("server.port")) ? "8080" : env.getProperty("server.port");

        String protocol = "http";
        log.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t{}://localhost:{}\n\t"
                        + "External: \t{}://{}:{}\n\t"
                        + "Swagger: \t{}://localhost:{}/doc.html\n\t"
                        + "Profile(s): \t{}\n----------------------------------------------------------",
                appName, protocol, port, protocol, hostAddress, port, protocol, port, env.getActiveProfiles());
    }
}
